/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package jktest;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JButton;

/**
 * 
 * A simple extension of the JButton class that includes an ActionListener and 
 * methods to communicate with the parent object when an action is performed.
 * 
 * @author Royce Feng
 * 
 */

public class JKButton extends JButton {
    
    private JKMainPanel parent;
    
    /**
     * 
     * The main constructor for this class. Usually called this way:
     * new JKButton( this, "buttonText" );
     * 
     * @param _parent The Parent Class 
     * @param buttonText The text the button should have on it
     */
    public JKButton( JKMainPanel _parent, String buttonText ) {
        super(buttonText);
        parent = _parent;
        this.addActionListener ( new JKButtonAction() );
    }
    
    /**
     * 
     * The ActionListener class that will call the ButtonClicked method when an
     * Action is performed.
     * 
     */
    private class JKButtonAction implements ActionListener {
        public void actionPerformed ( ActionEvent e ) {
            JKButton source = (JKButton)e.getSource();    
            String sourceText = source.getText();
            source.ButtonClicked( sourceText );
        }
    }
  
    /**
     * 
     * The ButtonClicked method that should never be called by another class,
     * which is why it's protected.
     * 
     * @param sourceText Usually the button string used as a source
     */
    protected void ButtonClicked ( String sourceText ) {
        parent.ButtonClicked( sourceText );
    }    

}
